package stringDemo;

import java.util.Arrays;

public class StringDemo {

    public static void main(String[] args) {
//        reverseString();//反转字符串
//        splitString();//分隔字符串返回字符串数组
//        replaceString();
        equalsDemo();//比较==和equals的区别
//        lowerCaseDemo();//大写字符转小写字符
        toCharArrayDemo();//字符串转字符数组
    }

    public static void toCharArrayDemo() {
        String str = "azx123";
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
    }

    public static void lowerCaseDemo() {
        String str = "ABC";
        str = str.toLowerCase();
        System.out.println(str);
    }


    /**
     * 反转字符串
     */
    public static void reverseString() {
        String str = "123456789";
        StringBuilder stringBuilder = new StringBuilder(str);//线程不安全，但是速度快
        stringBuilder = stringBuilder.reverse();
        System.out.println(stringBuilder);
    }

    /**
     * 把逗号分隔的字符串转化为数组
     */
    public static void splitString() {
        String str = "1,2,3,4,5";
        String[] strings = str.split(",");
        for (String str1 :
                strings) {
            System.out.print(str1);
        }
    }

    public static void replaceString() {
        String str = "1,2,3,4,5";
        str = str.replaceAll(",", "");
        System.out.println(str);
    }

    /**
     * 理解String对象
     */
    public static void equalsDemo() {
        String str1 = new String("1");
        String str2 = "1";
        System.out.println(str1 == str2);//false

        String str3 = new String("1") + new String("1");
        String str4 = new String("11");
        System.out.println(str3 == str4);//false
        System.out.println(str3.equals(str4));//true
        System.out.println(str3.intern() == str4.intern());//truesx

        //i被存放在常量池中
        //对象存在于堆中
        String str = "i";
        str2 = "i";
        str1 = new String("i");
        System.out.println(str.equals(str1));//true String类重写了equals方法，比较的是对象的内容
        System.out.println(str == str1);//false，==比较运算符，比较的对象为引用数据类型时，比较的是对象的地址值。
        System.out.println(str1 == str2);//false
    }
}
